package com.topica;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.topica.view.ZoomableImageView;

public class ImageSlideActivity extends Activity implements OnClickListener{
	public final static String PATH = "com.topica.ImageSlideActivity.PATH";
	private ZoomableImageView image;
	private ImageButton btnNext;
	private ImageButton btnBack;
	private TextView position;
	private int currentPos = 0;
	private int totalImage;
	private List<String> paths;
	private String imagePath;
	private String extra;
	private int w;
	private int h;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_slide_image);
		extra = getIntent().getStringExtra(PATH);
		System.out.println("extra=" + extra);
		if(null == extra || extra.equals(null)){
			Toast.makeText(this, R.string.no_image, Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		imagePath = Environment.getExternalStorageDirectory() + "/topica/" + extra;
		System.out.println("image path = " + imagePath);
		//imagePath = Environment.getExternalStorageDirectory() + getIntent().getStringExtra(PATH);
		image = (ZoomableImageView) findViewById(R.id.image);
		btnBack = (ImageButton) findViewById(R.id.btn_back);
		btnNext = (ImageButton) findViewById(R.id.btn_next);
		position = (TextView) findViewById(R.id.txt_position);
		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		h = metrics.heightPixels;
		w = metrics.widthPixels;

		paths = new ArrayList<String>();
		File f = new File(imagePath);
		if (!f.exists()) {
			f.mkdirs();
		}
		File[] list = f.listFiles();
		if(null != list && list.length > 0){
			totalImage = list.length -1;
		} else {
			Toast.makeText(ImageSlideActivity.this, R.string.no_image, Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		image.setMaxZoom(4f);
		String firstImage = imagePath + "b-" + Integer.valueOf(currentPos+1) + ".jpg";
		System.out.println(firstImage);
		image.setImageBitmap(decodeSampledBitmap(firstImage, w, h));
		position.setText(currentPos+1 + "/" + totalImage);
		btnNext.setOnClickListener(this);
		btnBack.setOnClickListener(this);
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}

	public static Bitmap decodeSampledBitmap(String path, int reqWidth, int reqHeight) {

		// First decode with inJustDecodeBounds=true to check dimensions
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(path, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options, reqWidth,
				reqHeight);

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(path, options);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_back:
			if(currentPos > 0){
				currentPos = currentPos - 1;
				image.setImageBitmap(decodeSampledBitmap(imagePath + "b-" + Integer.valueOf(currentPos+1) + ".jpg", w, h));
				position.setText(currentPos+1 + "/" + totalImage);
			} else {
				return;
			}
			break;

		case R.id.btn_next:
			if(currentPos < totalImage-1){
				currentPos = currentPos + 1;
				image.setImageBitmap(decodeSampledBitmap(imagePath + "b-" + Integer.valueOf(currentPos+1) + ".jpg", w, h));
				position.setText(currentPos+1 + "/" + totalImage);
			} else {
				return;
			}
			break;
		}
	}

}
